import type { CrawlProgress, CrawlResult, DownloadProgress } from './service'
import type {
  HttpRequestConfig,
  HttpResponse,
  LoginRequest,
  LoginResponse,
  RegisterRequest,
  RegisterResponse,
  SendCodeRequest,
  SendCodeResponse,
  LoginWithCodeRequest,
  AuthResponse,
  RegisterWithCodeRequest,
  ProfileListRequest,
  ProfileListResponse,
  ProfileCreateRequest,
  ProfileResponse,
  ProfileUpdateRequest,
  ProfileDeleteRequest,
  ProfileDeleteResponse,
  VideoListRequest,
  VideoListResponse,
  DouyinMainPageInfoRequest,
  DouyinMainPageInfoResponse
} from '../../common/types/http-ipc'

/**
 * API 服务接口定义
 * 定义所有可用的API方法，作为前端调用的契约
 * 返回类型为提取后的数据（handleHttpResponse处理后的结果）
 */
export interface IApiService {
  // 认证相关 - 返回处理后的数据
  login(phone: string, password: string): Promise<LoginResponse>
  register(phone: string, password: string, name?: string): Promise<RegisterResponse>
  sendCode(phone: string, type: number): Promise<SendCodeResponse>
  loginWithCode(phone: string, code: string): Promise<AuthResponse>
  registerWithCode(phone: string, code: string): Promise<AuthResponse>

  // 主页管理 - 返回处理后的数据
  getHomePages(): Promise<ProfileListResponse>
  addHomePage(homepage: ProfileCreateRequest): Promise<ProfileResponse>
  updateHomePage(id: string, updates: any): Promise<ProfileResponse>
  deleteHomePage(id: string): Promise<void>

  // 视频相关 - 返回处理后的数据
  getVideos(profileId?: string): Promise<VideoListResponse>

  // 抖音相关 - 返回处理后的数据
  getDouyinMainPageInfo(url: string): Promise<DouyinMainPageInfoResponse>
}

/**
 * Electron 原生功能接口定义
 * 定义Electron特有的功能（爬取、下载、系统功能）
 */
export interface IElectronService {
  // 爬取相关
  startCrawl(profileId: string): Promise<void>
  stopCrawl(profileId: string): Promise<void>

  // 视频下载
  downloadVideo(videoId: string): Promise<void>
  cancelDownload(downloadId: string): Promise<void>

  // 系统功能
  openExternal(url: string): Promise<void>
  showItemInFolder(path: string): Promise<void>

  // 事件监听
  onCrawlProgress(callback: (progress: CrawlProgress) => void): () => void
  onCrawlResult(callback: (result: CrawlResult) => void): () => void
  onDownloadProgress(callback: (progress: DownloadProgress) => void): () => void

  // 认证令牌管理
  setAuthToken(token: string | null): Promise<{ success: boolean }>
}

/**
 * 统一的服务接口
 * 组合API服务和Electron服务
 */
export interface IAppService extends IApiService, IElectronService {
  // 通用HTTP请求 - 返回完整响应
  httpRequest<T = any>(config: HttpRequestConfig): Promise<HttpResponse<T>>
}

/**
 * ElectronAPI接口定义
 * 主进程preload.js暴露的原始接口
 */
export interface ElectronAPI {
  // 统一的 HTTP API
  httpRequest<T = any>(config: HttpRequestConfig): Promise<HttpResponse<T>>

  api: {
    // 认证相关 - 返回完整HTTP响应
    login: (phone: string, password: string) => Promise<HttpResponse<LoginResponse>>
    register: (phone: string, password: string, name?: string) => Promise<HttpResponse<RegisterResponse>>
    sendCode: (phone: string, type: number) => Promise<HttpResponse<SendCodeResponse>>
    loginWithCode: (phone: string, code: string) => Promise<HttpResponse<AuthResponse>>
    registerWithCode: (phone: string, code: string) => Promise<HttpResponse<AuthResponse>>

    // 主页管理 - 返回完整HTTP响应
    getHomePages: () => Promise<HttpResponse<ProfileListResponse>>
    addHomePage: (homepage: ProfileCreateRequest) => Promise<HttpResponse<ProfileResponse>>
    updateHomePage: (id: string, updates: any) => Promise<HttpResponse<ProfileResponse>>
    deleteHomePage: (id: string) => Promise<HttpResponse<void>>

    // 视频相关 - 返回完整HTTP响应
    getVideos: (profileId?: string) => Promise<HttpResponse<VideoListResponse>>

    // 抖音相关 - 返回完整HTTP响应
    getDouyinMainPageInfo: (url: string) => Promise<HttpResponse<DouyinMainPageInfoResponse>>

    // 认证令牌管理
    setAuthToken: (token: string | null) => Promise<{ success: boolean }>
  }

  // Electron 原生功能
  startCrawl: (profileId: string) => Promise<void>
  stopCrawl: (profileId: string) => Promise<void>
  downloadVideo: (videoId: string) => Promise<void>
  cancelDownload: (downloadId: string) => Promise<void>
  openExternal: (url: string) => Promise<void>
  showItemInFolder: (path: string) => Promise<void>

  // 事件监听
  onCrawlProgress: (callback: (progress: CrawlProgress) => void) => () => void
  onCrawlResult: (callback: (result: CrawlResult) => void) => () => void
  onDownloadProgress: (callback: (progress: DownloadProgress) => void) => () => void
}

// 重新导出核心类型
export type {
  HttpRequestConfig,
  HttpResponse,
  // 请求类型
  LoginRequest,
  RegisterRequest,
  SendCodeRequest,
  LoginWithCodeRequest,
  RegisterWithCodeRequest,
  ProfileCreateRequest,
  ProfileUpdateRequest,
  ProfileDeleteRequest,
  ProfileListRequest,
  VideoListRequest,
  DouyinMainPageInfoRequest,
  // 响应类型
  LoginResponse,
  RegisterResponse,
  SendCodeResponse,
  AuthResponse,
  ProfileListResponse,
  ProfileResponse,
  ProfileDeleteResponse,
  VideoListResponse,
  DouyinMainPageInfoResponse
}